package com.dididoctor.doctor.Activity.Usercentre;

import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.Activity.Usercentre.Approve.ApproveActivity;
import com.dididoctor.doctor.Activity.Usercentre.Authentication.AuthenticationActivity;
import com.dididoctor.doctor.Activity.Usercentre.Evaluate.EvaluateActivity;
import com.dididoctor.doctor.Activity.Usercentre.InviteFriend.InviteFriendActivity;
import com.dididoctor.doctor.Activity.Usercentre.MyPurse.MyPurseActivity;
import com.dididoctor.doctor.Activity.Usercentre.MyService.MyServiceActiyity;
import com.dididoctor.doctor.Activity.Usercentre.Setting.UserSettingActivity;
import com.dididoctor.doctor.Activity.Usercentre.Userinfo.UserInfoActivity;
import com.dididoctor.doctor.Activity.Usercentre.Userinfo.Userinfo;
import com.dididoctor.doctor.Bean.Token;
import com.dididoctor.doctor.Http.MyHttpResponseHandler;
import com.dididoctor.doctor.MV.BusinessClient;
import com.dididoctor.doctor.MV.EduActivity;
import com.dididoctor.doctor.MV.Response;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Utils.LoaderImage;
import com.dididoctor.doctor.Utils.UserHeadImage;
import com.dididoctor.doctor.Utils.Util;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import org.apache.http.Header;

import java.util.Map;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： UserCentreActivity
 * @包名称：
 * @创建人： zhanjunjie
 * @创建时间：2016/4/1 15:51
 * @修改人：
 * @修改时间：2016/4/1 15:51
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class UserCentreActivity extends EduActivity {

    private TextView mTvUserName, mTvUserPhone;
    private ImageView mIvGender;
    private UserHeadImage headImage;
    private LoaderImage loaderImage;
    private static final int TO_USERINFO = 2001;
    private String isPass = "";

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_user_centre);
    }

    @Override
    public void init() {
        initview();

        initdata();

    }

    private void initdata() {
        mTvUserName.setText(Token.getName());
        mTvUserPhone.setText(Token.getPhone());
        loaderImage.load(headImage, Token.getHeadPic(), new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {
                headImage.setImageResource(R.drawable.img_userdefault);
            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {
                headImage.setImageResource(R.drawable.img_userdefault);
            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {

            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });

        //0女1男
        String sex = Token.getSex();
        if (!Util.isEmpty(sex)) {
            if (sex.equals("0")) {
                mIvGender.setImageResource(R.drawable.icon_girl);
            } else {
                mIvGender.setImageResource(R.drawable.icon_boy);
            }
        }
    }

    private void initview() {
        mTvUserName = (TextView) findViewById(R.id.tv_username);
        mTvUserPhone = (TextView) findViewById(R.id.tv_userphone);
        headImage = (UserHeadImage) findViewById(R.id.iv_head);
        mIvGender = (ImageView) findViewById(R.id.iv_gender);
        loaderImage = new LoaderImage(this);

        findViewById(R.id.lin_user_info).setOnClickListener(this);
        findViewById(R.id.lin_combo).setOnClickListener(this);
        findViewById(R.id.lin_consume).setOnClickListener(this);
        findViewById(R.id.lin_record).setOnClickListener(this);
        findViewById(R.id.lin_evaluate).setOnClickListener(this);
        findViewById(R.id.lin_collect).setOnClickListener(this);
        findViewById(R.id.lin_setting).setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mTvUserPhone.setText(Token.getPhone());
    }


    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            //个人信息
            case R.id.lin_user_info: {
                Intent intent = new Intent(this, UserInfoActivity.class);
                startActivityForResult(intent, TO_USERINFO);
            }
            break;
            //我的钱包
            case R.id.lin_combo: {
                Intent intent = new Intent(this, MyPurseActivity.class);
                startActivity(intent);
            }
            break;
            //我的服务
            case R.id.lin_consume: {
                Intent intent = new Intent(this, MyServiceActiyity.class);
                startActivity(intent);
            }
            break;
            //医生认证
            case R.id.lin_record: {
                getisPass();
            }
            break;
            //我的评价
            case R.id.lin_evaluate: {
                Intent intent = new Intent(this, EvaluateActivity.class);
                startActivity(intent);
            }
            break;
            //邀请好友
            case R.id.lin_collect: {
                Intent intent = new Intent(this, InviteFriendActivity.class);
                startActivity(intent);
            }
            break;
            //设置与帮助
            case R.id.lin_setting: {
                Intent intent = new Intent(this, UserSettingActivity.class);
                startActivity(intent);
            }
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TO_USERINFO && resultCode == RESULT_OK) {
            mTvUserPhone.setText(Token.getPhone());
            loaderImage.load(headImage, Token.getHeadPic(), new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String s, View view) {
                    headImage.setImageResource(R.drawable.img_userdefault);
                }

                @Override
                public void onLoadingFailed(String s, View view, FailReason failReason) {
                    headImage.setImageResource(R.drawable.img_userdefault);
                }

                @Override
                public void onLoadingComplete(String s, View view, Bitmap bitmap) {

                }

                @Override
                public void onLoadingCancelled(String s, View view) {

                }
            });
            mTvUserName.setText(Token.getName());
        }
    }

    public void getisPass() {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getUserId());
        params.put("token", Token.getToken());
        BusinessClient.post(ConstantValue.userDetail, params, new MyHttpResponseHandler() {

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    Map<String, Object> map = response.getMap("doctorInfo");
                    isPass = Util.toString(map.get("isPass"));
                    Token.setIsPass(isPass);
                    // 是否认证1-未认证2-申请认证3-认证成功4-认证失败
                    if ("4".equals(isPass)) {
                        Intent intent = new Intent(UserCentreActivity.this, AuthenticationActivity.class);
                        startActivity(intent);
                    } else {//审核中
                        Intent intent = new Intent(UserCentreActivity.this, ApproveActivity.class);
                        startActivity(intent);
                    }
                } else {
                    showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                showToastMessage("网络错误");
            }
        });
    }
}
